package com.byit.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.builder.MapBuilder;
import com.byit.constant.DefaultValue;
import com.byit.domain.AppMessageNotice;
import com.byit.jwt.ann.JwtUser;
import com.byit.jwt.domain.JwtSession;
import com.byit.msg.MessageResult;
import com.byit.service.IAppMessageNoticeService;
import com.byit.utils.PageModel;
import com.byit.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AppMessageNoticeController
 * @Description App 消息通知查询
 * @Author lxp
 * @Date 2019-01-10 17:15
 **/
@RestController
@RequestMapping("/app/messageNotice")
public class AppMessageNoticeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IAppMessageNoticeService appMessageNoticeService;

    /**
     * 按照条件查询消息通知、公告列表
     *
     * @param session
     * @param title
     * @param level
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public Object selectAppMessageNoticeAll(HttpSession session,
                                            HttpServletRequest request,
                                            @RequestParam(value = "title", defaultValue = "") String title,
                                            @RequestParam(value = "level", defaultValue = "") String level,
                                            @RequestParam(value = "type", defaultValue = "") String type,
                                            @RequestParam(value = "isCheck", defaultValue = "") String isCheck,
                                            @RequestParam(value = "publisher", defaultValue = "") String publisher,
                                            @RequestParam(value = "category", defaultValue = "") String category,
                                            @RequestParam(value = "page", defaultValue = DefaultValue.PAGE_FIRST) int page,
                                            @RequestParam(value = "limit", defaultValue = DefaultValue.PAGE_SIZE) int limit) {
        Page<AppMessageNotice> pageInfo = new Page<>(page, limit);
        Map<String, Object> params = MapBuilder.newMap(5)
                .setParam("title", title)
                .setParam("level", level)
                .setParam("type", type)
                .setParam("publisher", publisher)
                .setParam("category", category)
                .setParam("isCheck", isCheck).ok();
        if (null != title && !"".equals(title)) {
            params.put("title", "%" + StringUtil.decodeSpecialCharsWhenLikeUseBackslash(title) + "%");
        }

        Page<JSONObject> result = appMessageNoticeService.selectAppMessageNoticeAll(pageInfo, params, request);
        return MessageResult.success(result.getRecords(), PageModel.page(result));
    }

    /**
     * 消息通知详情
     */
    @GetMapping("/{id}.html")
    public Object detailsMessage(@PathVariable("id") Long id) {
        AppMessageNotice appMessageNotice = appMessageNoticeService.selectById(id);

        if (appMessageNotice == null) {
            return MessageResult.error();
        }
        String content = appMessageNotice.getContent();


        ModelAndView view = null;

        view = new ModelAndView("messageNotice");

        //跟新浏览量

        view.addObject("messageNotice", appMessageNotice);
        view.addObject("timestamp", new Date());
        return view;

    }

    /**
     * 消息通知信息
     */
    @GetMapping("/systemMessage")
    public Object getSystemMessage(@RequestParam(value = "category", defaultValue = "") String category,
                                   @RequestParam(value = "publisher", defaultValue = "") String publisher,
                                   HttpServletRequest request) {

        /**
         * 获取最新消息通知里的每个分类最新一条
         */
        Map<String, Object> params1 = MapBuilder.newMap(1)
                .setParam("category", "0")
                .ok();
        List<JSONObject> notice = appMessageNoticeService.selectNesestMessage(params1, request);
        Map<String, Object> params = MapBuilder.newMap(2)
                /*.setParam("category", "0")
                .setParam("publisher","1")*/
                .ok();
        Map<String, Object> map = new HashMap<>();
        // 消息通知里三天内的最重要的消息通知
        List<JSONObject> governmentNotice = appMessageNoticeService.selectSystemMessage(params, request);
        map.put("governmentNotice", governmentNotice);
        map.put("notice", notice);

        return MessageResult.success(map);
    }

    /**
     * 获取最新消息通知里的每个分类最新一条
     */
    @GetMapping("/newestMessage")
    public Object getNewestMessage(@RequestParam(value = "category", defaultValue = "") String category,
                                   @RequestParam(value = "type", defaultValue = "") String publisher,
                                   HttpServletRequest request) {
        Map<String, Object> params = MapBuilder.newMap(2)
                .setParam("category", category)
                .setParam("publisher", publisher)
                .ok();

        List<JSONObject> list = appMessageNoticeService.selectNesestMessage(params, request);

        return MessageResult.success(list);
    }

    /**
     * 逻辑删除消息通知信息
     *
     * @param session
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public Object delAppMessageNoticeByIds(@JwtUser JwtSession session, @RequestParam("ids") String ids) {
        // Long userId = SessionHelper.getUserId(session);
        Long userId = session.userId();
        List<Long> idList = StringUtil.splitLong(ids, ",");
        Map<String, Object> map = MapBuilder.newMap(3).setParam("updateTime", new Date()).setParam("operId", userId).ok();

        for (Long id : idList) {
            map.put("id", id);
            appMessageNoticeService.delAppMessageNoticeById(map);
        }
        return MessageResult.success();
    }


}